package com.fortune.cat.convert;

import com.fortune.cat.bean.FilterDefinitionRecipient;
import com.fortune.cat.bean.PredicateDefinitionRecipient;
import com.fortune.cat.bean.RouteDefinitionRecipient;
import com.fortune.cat.jooq.tables.pojos.TbFilterDefinitionRecipient;
import com.fortune.cat.jooq.tables.pojos.TbPredicateDefinitionRecipient;
import com.fortune.cat.jooq.tables.pojos.TbRouteDefinitionRecipient;
import com.fortune.cat.service.UserService;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhaozeyang
 * @date 2019/4/30 10:48
 * @description
 */
public class RouteConverts {


    private static final Gson GSON = new Gson();

    public static TbRouteDefinitionRecipient convertRoute(RouteDefinitionRecipient recipient) {
        TbRouteDefinitionRecipient route = new TbRouteDefinitionRecipient();
        BeanUtils.copyProperties(recipient, route);
        route.setCreator(UserService.currentUserId());
        route.setUri(recipient.getUri().toString());
        return route;
    }

    public static RouteDefinitionRecipient convertRouteRecipient(TbRouteDefinitionRecipient route) {
        RouteDefinitionRecipient result = new RouteDefinitionRecipient();
        BeanUtils.copyProperties(route, result);
        try {
            result.setUri(new URI(route.getUri()));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static List<RouteDefinitionRecipient> convertRouteRecipient(List<TbRouteDefinitionRecipient> list) {
        List<RouteDefinitionRecipient> resultList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(list)) {
            return resultList;
        }
        list.forEach(tbRouteDefinitionRecipient -> resultList.add(convertRouteRecipient(tbRouteDefinitionRecipient)));
        return resultList;
    }

    public static List<TbFilterDefinitionRecipient> convertFilter(List<FilterDefinitionRecipient> filters) {
        List<TbFilterDefinitionRecipient> filterList = new ArrayList<>();
        if (CollectionUtils.isEmpty(filters)) {
            return filterList;
        }
        filters.forEach(recipient -> {
            TbFilterDefinitionRecipient filter = new TbFilterDefinitionRecipient();
            BeanUtils.copyProperties(recipient, filter);
            filter.setCreator(UserService.currentUserId());
            filter.setArgs(GSON.toJson(recipient.getArgs()));
            filter.setExternal(recipient.isExternal() ? 1 : 0);
            filterList.add(filter);
        });
        return filterList;
    }

    public static List<TbPredicateDefinitionRecipient> convertPredicate(List<PredicateDefinitionRecipient> predicates) {
        List<TbPredicateDefinitionRecipient> predicateList = new ArrayList<>();
        if (CollectionUtils.isEmpty(predicates)) {
            return predicateList;
        }
        predicates.forEach(recipient -> {
            TbPredicateDefinitionRecipient predicate = new TbPredicateDefinitionRecipient();
            BeanUtils.copyProperties(recipient, predicate);
            predicate.setCreator(UserService.currentUserId());
            predicate.setArgs(GSON.toJson(recipient.getArgs()));
            predicateList.add(predicate);
        });
        return predicateList;
    }

    public static List<FilterDefinitionRecipient> convertFilterRecipient(List<TbFilterDefinitionRecipient> into) {
        List<FilterDefinitionRecipient> resultList = new ArrayList<>();
        if (CollectionUtils.isEmpty(into)) {
            return resultList;
        }
        into.forEach(recipient -> {
            FilterDefinitionRecipient filter = new FilterDefinitionRecipient();
            BeanUtils.copyProperties(recipient, filter);
            filter.setArgs(GSON.fromJson(recipient.getArgs(),new TypeToken<Map<String,String>>(){}.getType()));
            filter.setExternal(recipient.getExternal() == 1);
            resultList.add(filter);
        });
        return resultList;
    }

    public static List<PredicateDefinitionRecipient> convertPredicateRecipient(List<TbPredicateDefinitionRecipient> predicates) {
        List<PredicateDefinitionRecipient> resultList = new ArrayList<>();
        if (CollectionUtils.isEmpty(predicates)) {
            return resultList;
        }
        predicates.forEach(recipient -> {
            PredicateDefinitionRecipient predicate = new PredicateDefinitionRecipient();
            BeanUtils.copyProperties(recipient, predicate);
            predicate.setArgs(GSON.fromJson(recipient.getArgs(),new TypeToken<Map<String,String>>(){}.getType()));
            resultList.add(predicate);
        });
        return resultList;
    }
}
